﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;

namespace KMT.Controls.Common.Article
{
    public partial class ArticleLeft : UserControl
    {
        public int CategoryId { get; set; }
        protected int PageIndex;
        protected int PageSize;

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId = Utils.GetIdFromRequestString(Utils.CategoryId, CategoryId);
            PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize = Utils.GetIdFromRequestString(Utils.PageSize, SettingValues.PageSize_10);

            if (IsPostBack) return;
            ListPagingArticle();

            var controlLeft = (Categories)LoadControl("~/Controls/Common/Article/Categories.ascx");
            controlLeft.CategoryId = CategoryId;
            controlLeft.TypeId = 2;
            LeftCategory.Controls.Add(controlLeft);
        }

        public void ListPagingArticle()
        {
            //var totalRecords = 0;
            var lstArticle = ArticleController.Article_GetTopHighlight(10);
            //var lstArticle = ArticleController.Article_GetListOrderByPublishOn(CategoryId, PageIndex, PageSize, ref totalRecords);

            //var categoryName = CategoryController.GetCategoryName(CategoryId);

            if (lstArticle.Count <= 0) return;
            rptNews.DataSource = lstArticle;
            rptNews.DataBind();
        }

        protected void RptNewsItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item) return;

            var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
            var articleId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
            var title = ((HiddenField)e.Item.FindControl("hfTitle")).Value;

            var ltTitle = (Literal)e.Item.FindControl("ltTitle");
            var ltDate = (Literal)e.Item.FindControl("ltDate");
            var ltImage = (Literal)e.Item.FindControl("ltImage");

            var datePublish = ConvertUtility.ToDateTime((HiddenField)e.Item.FindControl("hfDate")).ToString("MMMM dd, yyyy");
            var categoryName = CategoryController.GetCategoryName(categoryId);

            var aHref = UrlHelpers.ArticleDetail(categoryName, ConvertUtility.ToInt32(articleId), title);

            var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=300", Constants.ThumbImage, articleId, (int)CategoryType.Article);

            ltDate.Text = datePublish;
            ltTitle.Text = string.Format("<a href=\"{0}\">{1}</a>", aHref, title);
            ltImage.Text = string.Format("<a href=\"{0}\" class=\"article-image\" style=\"background-image: url('{1}')\"></a>", aHref, imgHref);
        }
    }
}